﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WTSTC_App
{
    public partial class Restaurants : System.Web.UI.Page
    {
        private bool nameAndLoc;
        private bool phone;
        private bool storeNum;
        private bool addNew;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void locAndNameButton_Click(object sender, EventArgs e)
        {
            this.nameAndLoc = true;
            this.phone = false;
            this.storeNum = false;
            this.addNew = false;
            this.setUpSearchByComponents();
        }

        protected void phoneButton_Click(object sender, EventArgs e)
        {
            this.nameAndLoc = false;
            this.phone = true;
            this.storeNum = false;
            this.addNew = false;
            this.setUpSearchByComponents();
        }

        protected void storeNumButton_Click(object sender, EventArgs e)
        {
            this.nameAndLoc = false;
            this.phone = false;
            this.storeNum = true;
            this.addNew = false;
            this.setUpSearchByComponents();
        }

        protected void addNewResButton_Click(object sender, EventArgs e)
        {
            this.nameAndLoc = false;
            this.phone = false;
            this.storeNum = false;
            this.addNew = true;
            this.setUpSearchByComponents();
        }

        private void setUpSearchByComponents()
        {
            this.voteLabel.Visible = false;

            this.stateDropDownList.Visible = this.nameAndLoc;
            this.cityDropDownList.Visible = this.nameAndLoc;
            this.nameDropDownList.Visible = this.nameAndLoc;

            this.phoneDropDownList.Visible = this.phone;

            this.storeNumDropDownList.Visible = this.storeNum;

            this.CityTextBox.Visible = this.addNew;
            this.StateTextBox.Visible = this.addNew;
            this.NameTextBox.Visible = this.addNew;
            this.PhoneTextBox.Visible = this.addNew;
            this.stateLabel.Visible = this.addNew;
            this.cityLabel.Visible = this.addNew;
            this.nameLabel.Visible = this.addNew;
            this.phoneLabel.Visible = this.addNew;
            this.addButton.Visible = this.addNew;
            this.addMessageLabel.Visible = this.addNew;

            using (gnorris1Entities db = new gnorris1Entities())
            {
                var records = from u in db.RESTAURANTs select u;

                if (this.nameAndLoc)
                {
                    records = records.Where<RESTAURANT>(res => res.State == this.stateDropDownList.SelectedValue
                        && res.City == this.cityDropDownList.SelectedValue
                        && res.Name == this.nameDropDownList.SelectedValue);
                    populateTheGrid(records);
                }
                else if (this.phone)
                {
                    records = records.Where<RESTAURANT>(res => res.Phone == this.phoneDropDownList.SelectedValue);
                    populateTheGrid(records);
               
                }
                else if (this.storeNum)
                {
                    int storeNumber;
                    if (this.storeNumDropDownList.SelectedValue == "")
                    {
                        storeNumber = 1;

                    }
                    else
                    {
                        storeNumber = Convert.ToInt32(this.storeNumDropDownList.SelectedValue);
                    }

                    records = records.Where<RESTAURANT>(res => res.Store_Number == storeNumber);
                    populateTheGrid(records);

                }
                

            }
            
        }

        private void populateTheGrid(IQueryable<RESTAURANT> records)
        {
            this.resultsGridView.Width = 1000;


            this.resultsGridView.DataSource = records;
            this.resultsGridView.DataBind();

            if (this.resultsGridView.HeaderRow != null)
            {
                this.resultsGridView.HeaderRow.Cells[2].Text = "City";
                this.resultsGridView.HeaderRow.Cells[3].Text = "State";
                this.resultsGridView.HeaderRow.Cells[4].Text = "Name";
                this.resultsGridView.HeaderRow.Cells[5].Text = "Store Number";
                this.resultsGridView.HeaderRow.Cells[6].Text = "Phone Number";
                this.resultsGridView.HeaderRow.Cells[7].Text = "No Votes";
                this.resultsGridView.HeaderRow.Cells[8].Text = "Yes Votes";

            }
            
        }

        protected void resultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
            int currentRowIndex = Convert.ToInt32(e.CommandArgument);
            
            String storeNumber = this.resultsGridView.Rows[currentRowIndex].Cells[5].Text;

            String vote = e.CommandName;

            this.doVote(vote == "IncrementYesVotes", Convert.ToInt32(storeNumber));

        }

        private void doVote(bool voteYes, int storeNumber)
        {
            using (gnorris1Entities db = new gnorris1Entities())
            {
                var records = from u in db.RESTAURANTs select u;

                if (voteYes)
                {
                    RESTAURANT toUpdate = records.Single(res => res.Store_Number == storeNumber);
                    toUpdate.Yes_Votes = toUpdate.Yes_Votes + 1;
                    db.SaveChanges();
                }
                else
                {
                    RESTAURANT toUpdate = records.Single(res => res.Store_Number == storeNumber);
                    toUpdate.No_Votes = toUpdate.No_Votes + 1;
                    db.SaveChanges();
                }

                this.resultsGridView.DataBind();
                this.voteLabel.Text = "Thank you for voting.";
                this.voteLabel.Visible = true;
   
            }
 
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            String theCity = this.CityTextBox.Text;
            String theState = this.StateTextBox.Text.ToUpper();
            String theName = this.NameTextBox.Text;
            String thePhone = this.PhoneTextBox.Text;

            bool correctPhone = true;

            foreach (char c in thePhone)
            {
                if (!Char.IsDigit(c))
                {
                    correctPhone = false;
                }
            }

            if (theCity == "" || theState == "" || theName == "" || thePhone == "")
            {
                clearTextBoxes();
                this.addMessageLabel.Text = "Restaurant could not be added because of incomplete information.";
                return;
            }
            else if (theState.Length != 2)
            {
                this.addMessageLabel.Text = "State field must be exactly two characters.";
                return;
            }
            else if (thePhone.Length != 10)
            {
                this.addMessageLabel.Text = "Phone must be exactly 10 characters.";
                return;

            }
            else if (!correctPhone)
            {
                this.addMessageLabel.Text = "Phone must only contain digits.";
                return;
            }
            else
            {
                using (gnorris1Entities db = new gnorris1Entities())
                {
                    RESTAURANT newRes = new RESTAURANT();
                    newRes.City = theCity;
                    newRes.State = theState;
                    newRes.Name = theName;
                    newRes.Phone = thePhone;

                    db.AddToRESTAURANTs(newRes);
                    db.SaveChanges();

                    this.addMessageLabel.Text = "Restaurant has been added.";

                    this.stateDropDownList.DataBind();
                    this.clearTextBoxes();
                }
            }
        }

        private void clearTextBoxes()
        {
            this.CityTextBox.Text = "";
            this.StateTextBox.Text = "";
            this.NameTextBox.Text = "";
            this.PhoneTextBox.Text = "";
        }

    }
}